﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using MvcApplication5.Common;
using System.Data;

namespace MyFrameWork.Common
{
    public class CommonUtility
    {
        public static string StripEndingString(string target, params string[] stripList)
        {
            if (target.Length == 0) { return string.Empty; }
            if (target.LastIndexOf(stripList[0]) >= 0)
            {
                target = target.Remove(target.LastIndexOf(stripList[0]));
            }
            return target;
        }

        public static string ConvertListToString(List<string> targetList)
        {
            if (targetList.Count == 0)
            {
                return string.Empty;
            }

            string resultStr = string.Empty;

            foreach (string targetStr in targetList)
            {
                resultStr = resultStr + string.Format("'{0}',", targetStr);
            }

            resultStr = resultStr.Substring(0, resultStr.Length - 1);

            return resultStr;
        }

        public static ListCondition GetListCondition(System.Collections.Specialized.NameValueCollection targetList)
        {
            ListCondition listCondition = new ListCondition();


            foreach (string key in targetList.Keys)
            {
                if (key.LastIndexOf("SearchCondition") == 0)
                {
                    string conditionKey = key.Replace("SearchCondition", "").Replace("[", "").Replace("]", "");

                    if (conditionKey.Length == 0) { continue; }

                    listCondition.SearchCondition.Add(conditionKey, targetList[key]);
                }
            }

            PropertyInfo[] propertyList = listCondition.ListPager.GetType().GetProperties();

            foreach (PropertyInfo propertyInfo in propertyList)
            {
                if (targetList["ListPager[" + propertyInfo.Name + "]"] != null)
                {
                    switch (propertyInfo.PropertyType.Name)
                    {
                        case "Int32":
                            propertyInfo.SetValue(listCondition.ListPager, Convert.ToInt32(targetList["ListPager[" + propertyInfo.Name + "]"]), null);
                            break;
                        default:
                            propertyInfo.SetValue(listCondition.ListPager, targetList["ListPager[" + propertyInfo.Name + "]"].ToString(), null);
                            break;
                    }
                }
            }

            return listCondition;
        }

        public static string GetAjaxDataString()
        {
            string key = "ajaxData";

            string val = AppContext.Current.Request.QueryString[key];
            if (val == null)
                val = AppContext.Current.Request.Form[key];

            return val;
        }


        public static DbType GetDbType(object target)
        {
            string targetType = target.GetType().Name;

            switch (targetType)
            {
                case "String":
                case "string":
                    return DbType.String;

                case "Int32":
                    return DbType.Int32;

                case "DateTime":
                    return DbType.DateTime;

                case "Decimal":
                    return DbType.Decimal;

                case "Double":
                    return DbType.Double;

                default:
                    return DbType.Object;
            }
        }

        public static Object GetNotNullData(Object value, string typeName)
        {
            switch (typeName)
            {
                case "Decimal":
                case "Int32":
                case "Double":
                    return value == null || value == DBNull.Value ? 0 : value;

                case "Nullable`1":
                case "Datetime":
                    return value == null || value == DBNull.Value ? CmnConsts.NULL_DATE : Convert.ToDateTime(value);

                case "String":
                case "string":
                    return value == null || value == DBNull.Value ? String.Empty : value;

                default:
                    return value;
            }
        }


    }
}